package com.weiyuwqj.server.service.account.module;

import com.weiyuwqj.server.service.account.module.action.AbstractDealAction;
import com.weiyuwqj.server.mapper.BusAccountLogMapper;
import com.weiyuwqj.server.mapper.BusAccountMapper;
import com.weiyuwqj.api.model.BusAccount;
import com.weiyuwqj.api.model.BusAccountLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by fuxx on 15/11/25.
 */

@Component
public class DealModule {

    @Autowired
    BusAccountMapper accountMapper;
    @Autowired
    BusAccountLogMapper accountLogMapper;


    private int dealLog(BusAccountLog log) {
        BusAccount act = accountMapper.getBusAccountByUserId(log.getUserId());
        int account = accountMapper.dealAccount(act,
                log.getUsable(),
                log.getFrozen(),
                log.getWait(),
                log.getRepay(),
                log.getCash());
        log.setUsable(log.getUsable().add(act.getUsable()));
        log.setFrozen(log.getFrozen().add(act.getFrozen()));
        log.setWait(log.getWait().add(act.getWait()));
        log.setCash(log.getCash().add(act.getCash()));
        log.setRepay(log.getRepay().add(act.getRepay()));
        log.setTotal(log.getUsable()
                .add(log.getFrozen())
                .add(log.getWait())
        );
        log.setOperMoney(log.getOperMoney().abs());
        accountLogMapper.insert(log);
        return account;
    }

    public int dealAction(DealAction action) {
        AbstractDealAction act = (AbstractDealAction) action;
        return dealLog(act.getLog());
    }

}
